home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
asmexam.arc
/
DUMP.ASM
< prev
next >
Wrap
Assembly Source File
|
1984-09-03
|
11KB
|
315 lines
name dump
page 55,132
title DUMP --- Display File Contents
;DUMP --- a utility to display the contents of a file in hex
;and ASCII format. Requires PC-DOS or MS-DOS 2.0.
;Used in the form:
;A>dump path\filename.ext [>device]
;(item in square brackets is optional)
cr equ 0dh ;ASCII carriage return
lf equ 0ah ;ASCII line feed
blank equ 20h ;ASCII space code
command equ 80h ;buffer for command tail
blksize equ 128 ;size of input file records
output_handle equ 1 ;handle of standard output de
;(can be redirected)
error_handle equ 2 ;handle of standard error dev
;(not redirectable)
cseg segment para public 'CODE'
assume cs:cseg,ds:data,es:data,ss:stack
dump proc far ;entry point from PC-DOS
push ds ;save DS:0000 for final
xor ax,ax ;return to PC-DOS
push ax
mov ax,data ;make our data segment
mov es,ax ;addressable via ES register.
mov ah,30h ;check version of PC-DOS.
int 21h
cmp al,2
jae dump1 ;proceed, DOS 2.0 or greater.
mov dx,offset msg3 ;DOS 1.x --- print error mess
mov ax,es ;we must use the old PC-DOS
mov ds,ax ;string output function since
mov ah,9 ;handles are not available in
int 21h ;this version of PC-DOS.
ret
dump1: call get_filename ;get path and file spec.for
;input file from command line
mov ax,es ;set DS = ES for remainder
mov ds,ax ;of program.
jnc dump2 ;jump,got acceptable name.
mov dx,offset msg2 ;missing or illegal filespec.
mov cx,msg2_length
jmp dump9 ;print error message and exit
dump2: call open_input ;now try to open input file
jnc dump3 ;jump, opened input ok
mov dx,offset msg1 ;open of input file failed.
mov cx,msg1_length
jmp dump9 ;print error msg and exit.
dump3: call read_block ;initialize input file buffer
jnc dump4 ;jump,got a block
mov dx,offset msg4 ;empty file, print error
mov cx,msg4_length
jmp dump9 ;message and exit
;file successfully opened.
dump4: ;now convert and display it!
call get_char ;read 1 character from input.
jc dump8 ;jump,end of file
inc input_addr ;update relative file positio
or bx,bx ;is this 1st char of block?
jnz dump5 ;no
call print_heading
dump5: and bx,0fh ;is this first byte of 16?
jnz dump6 ;no, jump
push ax ;save the byte
mov di,offset output ;convert relative file addr.
mov ax,input_addr ;for output string
call conv_word
pop ax
dump6: ;store ASCII version of chara
;if it is alphanumeric,
mov di,offset outputb
add di,bx ;calculate output string addr
mov byte ptr[di],'.' ;if it is control character,
cmp al,blank ;just print a dot.
jb dump7 ;jump,not alphanumeric,
cmp al,7eh
ja dump7 ;jump not alphanumberic,
mov [di],al ;store ASCII character.
dump7:
push bx ; save current offset
mov di,offset outputa ; offset of hex area
add di,bx ; + 3*bx is where next
add di,bx ; pair of hex digits go
add di,bx
call conv_byte ;value => printable hex digits
pop bx ;restore offset value
cmp bx,0fh ;16 bytes converted?
jne dump4 ; no go back and do another
mov dx,offset output ;
mov cx,output_length
call write_std
jmp dump4
dump8:
call close_input
ret
dump9:
call write_error
ret
dump endp
get_filename proc near
mov si,offset command
mov di,offset input_name
cld
lodsb
or al,al
jz get_filename4
get_filename1:
lodsb
cmp al,cr
je get_filename4
cmp al,blank
jz get_filename1
get_filename2:
stosb
lodsb
cmp al,cr
je get_filename3
cmp al,blank
jne get_filename2
get_filename3:
clc
ret
get_filename4:
stc
ret
get_filename endp
open_input proc near
mov dx,offset input_name ; ds:dx => addr filename
mov al,0
mov ah,3dh ; open file function in DOS
int 21h
mov input_handle,ax
ret
open_input endp
close_input proc near
mov bx,input_handle
mov ah,3eh ; DOS closefile function number
int 21h
ret
close_input endp
get_char proc near
mov bx,input_ptr
cmp bx,blksize ; is this the end of a block?
jne get_char1
mov input_ptr,0 ; if eob then set pointer to 0
call read_block ; and get another block
jnc get_char
ret
get_char1: mov al,[input_buffer+bx]
inc input_ptr
clc ; set carry 0 since not eof
ret
get_char endp
read_block proc near
mov bx,input_handle
mov cx,blksize
mov dx,offset input_buffer
mov ah,3fh ; dos read disk function
int 21h
inc input_block
mov input_ptr,0
or ax,ax ;ax contains # of bytes read
jnz read_block1 ; if not eof ret (carry is 0)
stc ; else set carry and ret
read_block1:
ret
read_block endp
write_std proc near
mov bx,output_handle
mov ah,40h ; write device function number
int 21h
ret
write_std endp
write_error proc near
mov bx,error_handle
mov ah,40h
int 21h
ret
write_error endp
print_heading proc near
push ax
push bx
mov di,offset headinga
mov ax,input_block
call conv_word
mov dx,offset heading
mov cx,heading_length
call write_std
pop bx
pop ax
ret
print_heading endp
conv_word proc near
push ax
mov al,ah
call conv_byte
pop ax
call conv_byte
ret
conv_word endp
conv_byte proc near
sub ah,ah ; zero ah reg
mov cl,16
div cl
call ascii
stosb
mov al,ah
call ascii
stosb
ret
conv_byte endp
ascii proc near
add al,'0' ; convert al to ascii char
cmp al,'9'
jle ascii2
add al,'a'-'9'-1 ; offset to a-f hex range
ascii2: ret
ascii endp
cseg ends
data segment para public'DATA'
input_name db 64 dup(0) ; buffer for input filespec
input_handle dw 0 ;token from DOS for input file
input_ptr dw 0 ;pointer to input de-blocking buffer
input_addr dw 0 ; relatve address in file
input_block dw 0 ;current 128k block byte number
output db 'nnnn',blank,blank
outputa db 16 dup('00',blank)
db blank
outputb db '0123456789abcdef',cr,lf
output_length equ $-output
heading db cr,lf,'Record',blank
headinga db 'nnnn',blank,blank,cr,lf
db 7 dup(blank)
db '0 1 2 3 4 5 6 7 '
db '8 9 A B C D E F',cr,lf
heading_length equ $-heading
input_buffer db blksize dup(?)
msg1 db cr,lf
db 'Cannot open file'
msg1_length equ $-msg1
msg2 db cr,lf
db 'Missing file name'
msg2_length equ $-msg2
msg3 db cr,lf
db 'Requires DOS 2.0 or greater'
msg3_length equ $-msg3
msg4 db cr,lf
db 'Empty file',cr,lf
msg4_length equ $-msg4
data ends
stack segment para stack'STACK'
db 64 dup(?)
stack ends
end dump